package org.angelica.aliyun.manager;

import com.aliyun.opensearch.sdk.dependencies.com.google.common.collect.Lists;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.ObjectMetadata;
import org.angelica.aliyun.entity.StsToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.List;

/**
 * 云存储服务
 * @author aizhimin
 */
public class OssManager {
    private static final Logger logger = LoggerFactory.getLogger(OssManager.class);

    private static OssManager instance = new OssManager();
    private OssManager(){ }

    private static StsToken ossStsToken = null;
    private static OSSClient client = null;
    public static OssManager getInstance(String endpoint,StsToken stsToken){
        //token过期，则重新创建SyncClient
        if(ossStsToken != null && !ossStsToken.getSecurityToken().equals(stsToken.getSecurityToken())){
            if(client != null){
                client.shutdown();
            }
            client = null;
        }

        if(client == null){
            ossStsToken = stsToken;
            CredentialsProvider credsProvider = new DefaultCredentialProvider(
                    stsToken.getAccessKeyId(),
                    stsToken.getAccessKeySecret(),
                    stsToken.getSecurityToken());
            client = new OSSClient(endpoint,credsProvider,null);
        }
        return instance;
    }

    /**
     * 上传文件
     * @param bucketName
     * @param objectName
     * @param inputStream
     * @return
     */
    public String uploadFile(String bucketName,String objectName, InputStream inputStream) {
        try{
            client.putObject(bucketName, objectName, inputStream);
        }catch(Exception e){
            logger.error(e.getMessage());
        }
        return objectName;
    }

    /**
     * 上传文件
     * @param bucketName
     * @param objectName
     * @param inputStream
     * @return
     */
    public String uploadFile(String bucketName,String objectName, InputStream inputStream,ObjectMetadata meta) {
        try{
            client.putObject(bucketName, objectName, inputStream,meta);
        }catch(Exception e){
            logger.error(e.getMessage());
        }
        return objectName;
    }

    /**
     * 判断文件是否存在
     * @param bucketName
     * @param objectName
     * @return
     */
    public boolean doesFileExist(String bucketName,String objectName) {
        try{
            return client.doesObjectExist(bucketName, objectName);
        }catch(Exception e){
            logger.error(e.getMessage());
        }
        return false;
    }

    /**
     * 删除文件
     * @param bucketName
     * @param objectName
     */
    public void deleteFile(String bucketName, String objectName) {
        try{
            client.deleteObject(bucketName, objectName);
        }catch(Exception e){
            logger.error(e.getMessage());
        }
    }

    /**
     * 获取文件的全部元信息
     * @param bucketName
     * @param objectName
     * @return
     */
    public ObjectMetadata getObjectMetadata(String bucketName, String objectName){
        return client.getObjectMetadata(bucketName,objectName);
    }

    /**
     * 获取指定目录下的所有文件
     * @param bucketName
     * @param prefix
     * @return
     */
    public List<String> listObjects(String bucketName,String prefix){
        List<String> keys = Lists.newArrayList();
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
        // 设置prefix参数来获取fun目录下的所有文件。
        listObjectsRequest.setPrefix(prefix);
        //最多返回2百个
        listObjectsRequest.setMaxKeys(200);
        ObjectListing objectListing = client.listObjects(listObjectsRequest);
        for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            keys.add(objectSummary.getKey());
        }
        return keys;
    }

}
